package com.ccsu.mymarket.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ccsu.mymarket.controller.utils.DataUtils;
import com.ccsu.mymarket.entity.Book;
import com.ccsu.mymarket.entity.Catagory;
import com.ccsu.mymarket.entity.message.Meta;
import com.ccsu.mymarket.entity.message.Result;
import com.ccsu.mymarket.exception.MyException;
import com.ccsu.mymarket.jwt.UserLoginToken;
import com.ccsu.mymarket.service.BookService;
import com.ccsu.mymarket.service.CatagoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/categories")
public class CatagoryController {
    private final CatagoryService catagoryService;
    private final BookService bookService;

    public CatagoryController(@Autowired CatagoryService catagoryService,@Autowired BookService bookService) {
        this.catagoryService = catagoryService;
        this.bookService = bookService;
    }

    @GetMapping("/type/{type}")
//    传入分类级别进行搜索, 返回的分类对象仅有分类id 和 名字
    public Result getCatagoryByType(@PathVariable Integer type) {
        List<Catagory> catagoryList = catagoryService.getCatagory(type);
        if (catagoryList == null)
            return new Result(new Meta(201, "暂无数据"), null);
        return new Result(new Meta(200, "查询成功！"), catagoryList);
    }

    @GetMapping("/all")
//    获取所有分类，返回树形结构
    public Result getAllCatagory() {
        List<Catagory> catagoryList = DataUtils.getCatagTree(catagoryService.list());
        if (catagoryList == null)
            return new Result(new Meta(201, "暂无数据"), null);
        return new Result(new Meta(200, "查询成功！"), catagoryList);
    }

    @UserLoginToken
    @PostMapping("/add")
//    依照Catagory实体类格式传入所有必需数据
    public Result addCCatagory(@RequestBody Catagory catagory) {
        if (catagory == null)
            return new Result(new Meta(403, "未接收到新增数据！"), null);
        if (catagoryService.save(catagory))
            return new Result(new Meta(200, "添加分类成功！"), null);
        return new Result(new Meta(500, "操作失败！"), null);
    }

    @GetMapping("/search/{id}/{type}")
//   用分类id和级别查询图书
    public Result searchBookByCatagory(@PathVariable Integer id,@PathVariable Integer type) throws MyException {
        if (id == null)
            return new Result(new Meta(500, "请输入查询id！"), null);
        List<Book> bookList= catagoryService.searchBookByCatagory(id,type);
        if (bookList != null)
            return new Result(new Meta(200, "查询成功！"), bookList);
        return new Result(new Meta(201, "暂无相关数据"),null);
    }

    @UserLoginToken
    @DeleteMapping("/delete/{id}")
    //    分类id
    public Result deleteCatagory(@PathVariable Integer id) {
        Catagory catagory = catagoryService.getOne(new LambdaQueryWrapper<Catagory>()
                .eq(Catagory::getId, id).select(Catagory::getCataType));
        if (catagory == null)
            return new Result(new Meta(403, "未查找到该分类！"), null);

//        查看该分类或其子分类是否关联商品
        if (catagoryService.ClassChildBookExist(id, catagory.getCataType())) {
            return new Result(new Meta(403, "有商品关联该分类，无法删除！"), null);
        } else {
            if (catagoryService.deleteCatagory(id, catagory.getCataType()))
                return new Result(new Meta(200, "商品分类删除成功！"), null);
        }
        return new Result(new Meta(500, "操作失败！"), null);
    }

    @UserLoginToken
    @PutMapping("/update")
//    依照Catagory实体类格式传入 id 和新的分类名，用以修改分类名
    public Result updateCatagory(@RequestBody Catagory catagory) {
        if (catagory.getId() == null)
            return new Result(new Meta(403, "请输入分类id！"), null);
        if (catagoryService.update(new LambdaUpdateWrapper<Catagory>()
                .eq(Catagory::getId, catagory.getId())
                .set(catagory.getCatagName() != null, Catagory::getCatagName, catagory.getCatagName()))
        ) {
            return new Result(new Meta(200, "商品分类更新成功！"), null);
        }
        return new Result(new Meta(500, "操作失败！"), null);
    }
}
